package com.ihg.hiex.admin.web.controller.account;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.persistence.support.QueryOrder;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.core.web.utils.WebUtils;
import com.ihg.hiex.admin.entity.search.AssessorSearch;
import com.ihg.hiex.admin.service.account.AssessorService;
import com.ihg.hiex.admin.service.session.SessionService;
import com.ihg.hiex.entity.session.Session;
import com.ihg.hiex.entity.session.Status;
import com.ihg.hiex.entity.user.Assessor;

/**
 * 
 * @author ZEAL
 *
 */
@Controller
@RequestMapping("/assessors")
@MenuSetting("menu-settings")
public class AssessorController {

    private static final Logger logger = LoggerFactory.getLogger(AssessorController.class);
    
    @Autowired
    private AssessorService assessorService;
    
    @Autowired
    private SessionService sessionService;
    
    /**
     * list assessor by page
     * 
     * @param page
     * @return
     */
    @MenuSetting("submenu-assessor-management")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView list(Page page) {
        
        ModelAndView modelAndView = new ModelAndView("account/assessor/assessor-list");
        
        int totals = assessorService.count(Assessor.class);
        if (totals > 0) {
            
            QueryOrder queryOrder = new QueryOrder("lastModifyTime", "DESC");
            List<Assessor> assessors = assessorService.listByPage(Assessor.class, queryOrder, page.with(totals));
            
            modelAndView.addObject("assessors",assessors);
            modelAndView.addObject("page", page);
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * display search panel
     * 
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public String search() {
        return "account/assessor/assessor-search";
    }

    /**
     * on search assessor
     * 
     * @param search
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-assessor-management")
    public ModelAndView onSearch(AssessorSearch search, HttpServletRequest request, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("account/assessor/assessor-list");
        
        int totals = assessorService.count(Assessor.class, search.getPreparedFilter(false));
        if (totals > 0) {
            
            List<Assessor> assessors = assessorService.search(Assessor.class, search, page.with(totals));
            modelAndView.addObject("assessors",assessors);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString",WebUtils.extractQueryString(request, "page","filter"));

        }
        
        return modelAndView;
        
    }
    
    /**
     * view assessor
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}" , method = RequestMethod.GET)
    @MenuSetting("submenu-assessor-management")
    public ModelAndView view(@PathVariable("id") Long id) {
        
        ModelAndView modelAndView = new ModelAndView("account/assessor/assessor-view");

        // 1. load assessor info
        Assessor assessor = assessorService.load(Assessor.class, id);
        modelAndView.addObject("assessor", assessor);
        
        // 2. list active session
        List<Session> activeSessions = sessionService.list(Status.ACTIVE, assessor);
        modelAndView.addObject("activeSessions", activeSessions);
        
        // 3. list history session
        List<Session> historySessions = sessionService.list(Status.HISTORY, assessor);
        modelAndView.addObject("historySessions", historySessions);
        
        return modelAndView;
        
    }
    
    /**
     * prepare to create assessor
     * 
     * @return
     */
    @RequestMapping(value = "/create" , method = RequestMethod.GET)
    public String create() {
        return "account/assessor/assessor-create";
    }
    
    /**
     * create a new assessor
     * 
     * @param assessor
     * @return
     */
    @RequestMapping(value = "" , method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onCreate(Assessor assessor) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            assessorService.create(assessor);
            
            httpResult.success().message("增加成功");
            
        } catch (Exception e) {
            httpResult.fail("500").message("增加失败");
            logger.error("#onCreate() - create fail, exception is [{}]",e);
        }
        
        return httpResult;
        
    }
    
    /**
     * display edit panel
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/edit" , method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("id") Long id) {
        
        ModelAndView modelAndView = new ModelAndView("account/assessor/assessor-edit");
        
        Assessor assessor = assessorService.load(Assessor.class, id);
        
        modelAndView.addObject("assessor", assessor);
        
        return modelAndView;
        
    }
    
    /**
     * edit an assessor
     * 
     * @param assessor
     * @return
     */
    @RequestMapping(value = "/{id}" , method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEdit(@Preloading("assessorId") Assessor assessor) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            assessorService.edit(assessor);
            
            httpResult.success().message("修改成功");
            
        } catch (Exception e) {
            httpResult.fail("500").message("修改失败");
            logger.error("#onEdit() - edit fail, exception is [{}]",e);
        }
        
        return httpResult;
        
    }
    
    /**
     * display delete panel
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/delete" , method = RequestMethod.GET)
    public ModelAndView delete(@PathVariable("id") Long id) {
        
        ModelAndView modelAndView = new ModelAndView("account/assessor/assessor-delete");
        
        modelAndView.addObject("id", id);
        
        return modelAndView;
        
    }
    
    /**
     * delete assessor
     * 
     * @param assessorId
     * @return
     */
    @RequestMapping(value = "/{assessorId}" , method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onDelete(@PathVariable("assessorId") Long assessorId) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            Assessor assessor = assessorService.load(Assessor.class, assessorId);
            assessorService.disable(assessor);
            
            httpResult.success().message("删除成功");
            
        } catch (Exception e) {
            httpResult.fail("500").message("删除失败");
            logger.error("#onDelete() - delete fail, exception is [{}]",e);
        }
        
        return httpResult;
        
    }
    
}